package com.frxuexi.park2212.hourse

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.MyHourseBean
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityHourseBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/HourseActivity")
@AndroidEntryPoint
class HourseActivity : BaseActivity<ActivityHourseBinding, HourseViewModel>() {
    val hourseAreaAdapter by lazy { HourseAreaAdapter(vm) }
    override fun initData() {
        vm.getHourses.observe(this) {
            hourseAreaAdapter.data.clear()
            val map = it.groupBy { it.area }//根据面积分组
            val list = map.keys.sorted()//根据面积排序

            val hourseBeans = mutableListOf<MyHourseBean>()
            var index=1
            for (key in list){
                hourseBeans.add(MyHourseBean(index,key,map.get(key)!!))
                index++
            }
            hourseAreaAdapter.addData(hourseBeans)
            hourseAreaAdapter.notifyDataSetChanged()

        }

        vm.rent.observe(this){
            ToastUtils.showLong("操作成功")
        }
    }

    override fun initView() {
       binding.rvHouse.apply {
            adapter = hourseAreaAdapter
            layoutManager = LinearLayoutManager(this@HourseActivity)
        }

        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.rb_a) {
                vm.getHourseByRid(1)
            } else if (checkedId == R.id.rb_b) {
                vm.getHourseByRid(2)
            } else if (checkedId == R.id.rb_c) {
                vm.getHourseByRid(3)
            } else if (checkedId == R.id.rb_d) {
                vm.getHourseByRid(4)
            }
        }
    }

    override fun onResume() {
        super.onResume()
//        vm.getHourses()
        vm.getHourseByRid(1)
    }

}